/**
 * 查询地图范围
 */
export function queryMapRange() {
    const center = gaodeMap.getCenter()
    const wgsCenter = GCJ2WGS(center)
    const zoom = gaodeMap.getZoom()
    const bounds = gaodeMap.getBounds()
    const height = calculateHeightFromBounds(bounds)
    const size = calculateSizeFromHeight(height)
    //获取地图当前俯仰角  0对应cesium的-90
    const pitch = gaodeMap.getPitch() - 90
    //获取地图顺时针旋转角度, 范围: [0 ~ 360]   cesium为逆时针旋转范围: [0 ~ 360]
    const rotation = gaodeMap.getRotation()
    const heading = rotation2Heading(rotation)
    return {longitude: wgsCenter.lng, latitude: wgsCenter.lat, size, zoom, height, pitch, heading: heading, roll: 0}
}

/**
 * 根据给定的地图范围计算场景的高度
 * @param bounds 地图范围
 * @returns {number}场景高度
 */
export function calculateHeightFromBounds(bounds) {
    const northWest = bounds.getNorthWest()
    const southEast = bounds.getSouthEast()
    var _PI = 3.1415926
    var _earthRadius = 6378137
    var altitude = _earthRadius
    var boundsWidth = southEast.lng - northWest.lng
    if (boundsWidth >= 120) {
        altitude = _earthRadius * boundsWidth / 60 - _earthRadius
    } else if (boundsWidth != 0) {
        var angle1 = (boundsWidth / 360) * _PI
        var height = Math.sin(angle1) * _earthRadius
        var a = height / Math.tan(angle1)
        var b = height / Math.tan(_PI / 6)
        altitude = a + b - _earthRadius
    }
    return altitude
}

/**
 * 根据给定的场景高度计算地图中显示范围的宽度
 * @param height 场景高度
 * @returns {number} 地图显示范围尺寸
 * @private
 */
export function calculateSizeFromHeight(height) {
    var _PI = 3.1415926
    var _earthRadius = 6378137
    var size
    if (height >= _earthRadius) {
        // 当场景高度大于可全幅显示整球的高度时
        var ratio = (height + _earthRadius) * 0.5
        size = 120 * ratio / _earthRadius
    } else {
        // 当场景高度小于可全幅显示整球的高度时，即无法看到整球时
        var tan30 = Math.tan(_PI / 6)
        // 设置方程组的a,b,c
        var a = (Math.pow(tan30, 2) + 1) * Math.pow(_earthRadius, 2)
        var b = -2 * (_earthRadius + height) * _earthRadius * Math.pow(tan30, 2)
        var c = Math.pow(tan30, 2) * Math.pow(_earthRadius + height, 2) - Math.pow(_earthRadius, 2.0)
        // 解一元二次方程，取锐角，因此余弦值较大
        var cosd = (-b + Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a)
        var d = Math.acos(cosd)
        var widthd = 2 * d * _earthRadius
        size = (widthd / (_PI * _earthRadius)) * 180
    }
    return size
}

export function GCJ2WGS(location) {
    const lon = location.lng;
    const lat = location.lat;
    const a = 6378245.0
    const ee = 0.00669342162296594323
    const PI = Math.PI
    let x = lon - 105.0
    let y = lat - 35.0
    let dLon;
    dLon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
    dLon += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
    dLon += (20.0 * Math.sin(x * PI) + 40.0 * Math.sin(x / 3.0 * PI)) * 2.0 / 3.0;
    dLon += (150.0 * Math.sin(x / 12.0 * PI) + 300.0 * Math.sin(x / 30.0 * PI)) * 2.0 / 3.0;
    let dLat;
    dLat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
    dLat += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
    dLat += (20.0 * Math.sin(y * PI) + 40.0 * Math.sin(y / 3.0 * PI)) * 2.0 / 3.0;
    dLat += (160.0 * Math.sin(y / 12.0 * PI) + 320 * Math.sin(y * PI / 30.0)) * 2.0 / 3.0;
    let radLat;
    radLat = lat / 180.0 * PI
    let magic;
    magic = Math.sin(radLat)
    magic = 1 - ee * magic * magic
    let sqrtMagic;
    sqrtMagic = Math.sqrt(magic)
    dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI);
    dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * PI);
    let wgsLon = lon - dLon
    let wgsLat = lat - dLat
    return {lng: wgsLon, lat: wgsLat}
}

export function rotation2Heading(rotation) {
    let heading;
    if (rotation === 0) {
        heading = 360
    } else if (rotation > 0) {
        heading = 360 - rotation
    } else {
        heading = Math.abs(rotation)
    }
    return heading
}
